<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends Controller{
    public function catelist(){
        $goods = M('Goods');
        $cate = M('Cat');
        $catlist = $cate->field('cat_id,cat_name,parent_id')->select();
        $alist = catList($catlist);
        $this->assign('alist',$alist);
        $this->display();
    }

    public function cateadd(){

        $cate = D('Cat');
        $catlist = $cate->field('cat_id,cat_name,parent_id')->select();
        $alist = catList($catlist);
        $this->assign('alist',$alist);

        if($_POST){
            // if (!$goods->create()){
            //     // 如果创建失败 表示验证没有通过 输出错误提示信息
            //     exit($goods->getError());
            // }else{
            //     $goods->create();
            //     $goods->add();
            //     echo '<script>alert("very good!")</script>';
            // }
            $cate->cat_name = I('cat_name');
            $cate->parent_id = I('parent_id');
            if($cate->add()){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->display();
        }
    }

    public function del(){
        $cate = D('Cat');
        $cat_id = I('cat_id');
        //var_dump($cate->where('cat_id='.$cat_id)->delete());
        $result = $cate->where('cat_id='.$cat_id)->delete();
        if($result){
             //echo '<script>alert("very good!")</script>';
            // echo '<script>window.location.href=__URL__/addlist; </script>';
            $this->success('删除成功');
        }else{
            //echo '<script>alert("删除失败")</script>';
            //header('Location:'.__URL__.'/addlist');
            //echo '<script>window.location.href='.__ROOT__.'/addlist; </script>';
            $this->error('删除失败');
        }
    }

    public function edit(){
        $cate = D('Cat');

        if(!empty($_POST)){
            $cat_id = $_GET['cat_id'];
            $cat = $cate->create();
            //print_r($cate->create());
            $result = $cate->where('cat_id='.$cat['cat_id'])->save();
            if($result){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
            //echo $cat_id;
        }else{
            //echo 234;
            $cat_id = I('cat_id');
            $catlist = $cate->field('cat_id,cat_name,parent_id')->select();
            $alist = catList($catlist);
            $this->assign('alist',$alist);



            $catedata = $cate->select();
            //var_dump($catedata);
            // /$cat_name = $catedata[0];//['cat_name'];
            //echo $cat_name;exit();
            $this->assign('catedata',$catedata[0]);
            $this->display();
        }
    }
}
?>
